package cn.haona.game.simb.wap.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import cn.haona.game.simb.object.bio.User;
import cn.haona.game.simb.servlet.Define;
import cn.haona.game.simb.servlet.SessionHn;
import cn.haona.game.simb.wap.AppDefine;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 登录拦截器---防止未登录用户进行游戏
 * @author Administrator
 *
 */
public class LoginInterceptor extends AbstractInterceptor {

	public LoginInterceptor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("通过Login拦截器------");
		boolean isLogined = false;
		// 得到ServletRequest;
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);

		// SessionHn
		SessionHn session = (SessionHn) request.getAttribute(Define.SESSION_ID);
		if (session != null) {
			// 取的User object
			User user = (User) session
					.getElement(SessionHn.KEY_USERBEAN);
			//如果有user object
			if(user!=null)
			{
				isLogined=true;
			}
		}

		//如果login
		if (isLogined) {
			return arg0.invoke();
		}
		//没login
		else {
			return "login";
		}
	}

}
